// The contents of this file are subject to the Mozilla Public License Version
// 1.1
//(the "License"); you may not use this file except in compliance with the
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
//
//Software distributed under the License is distributed on an "AS IS" basis,
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
//for the specific language governing rights and
//limitations under the License.
//
//The Original Code is "The Columba Project"
//
//The Initial Developers of the Original Code are Frederik Dietz and Timo
// Stich.
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
//
//All Rights Reserved.
package org.columba.mail.gui.config.filter.plugins;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JComponent;
import javax.swing.JPanel;

import org.columba.api.gui.frame.IFrameMediator;
import org.columba.api.plugin.IExtensionHandler;
import org.columba.api.plugin.IExtensionInterface;
import org.columba.api.plugin.PluginHandlerNotFoundException;
import org.columba.core.filter.FilterAction;
import org.columba.core.filter.IFilterAction;
import org.columba.core.gui.base.ComboMenu;
import org.columba.core.gui.dialog.ErrorDialog;
import org.columba.core.plugin.PluginManager;
import org.columba.mail.gui.config.filter.ActionList;
import org.columba.mail.plugin.IExtensionHandlerKeys;

public class DefaultActionRow implements IExtensionInterface {
	protected JPanel panel;

	protected IFilterAction filterAction;

	protected GridBagLayout gridbag = new GridBagLayout();

	protected GridBagConstraints c = new GridBagConstraints();

	protected ActionList actionList;

	protected int count;

	private IFrameMediator mediator;

	private ComboMenu comboMenu;

	public DefaultActionRow(IFrameMediator mediator, ActionList list,
			IFilterAction action) {
		this.filterAction = action;
		this.actionList = list;
		this.mediator = mediator;

		panel = new JPanel();

		initComponents();

		updateComponents(true);

	}

	public JPanel getContentPane() {
		return panel;
	}

	public void updateComponents(boolean b) {
		if (b) {
			String name = (String) filterAction.getAction();
			comboMenu.setText(name);
		} else {
			String name = (String) comboMenu.getText();
			filterAction.setAction(name);
		}
	}

	public void initComponents() {
		panel.removeAll();

		panel.setLayout(gridbag);

		IExtensionHandler pluginHandler = null;

		try {
			pluginHandler = PluginManager
					.getInstance().getExtensionHandler(IExtensionHandlerKeys.ORG_COLUMBA_MAIL_FILTERACTION);
		} catch (PluginHandlerNotFoundException ex) {
			ErrorDialog.createDialog(ex.getMessage(), ex);
		}

		
		comboMenu = new ComboMenu();
		String[] ids = pluginHandler.getPluginIdList();
		for (String element : ids) {
			// TODO localize filter action names
			comboMenu.addMenuItem(element, element);
		}
		
		comboMenu.setText(filterAction.getAction());
		comboMenu.addItemListener(actionList);

		c.fill = GridBagConstraints.VERTICAL;
		c.weightx = 1.0;
		c.insets = new Insets(2, 2, 2, 2);
		c.gridx = 0;
		c.anchor = GridBagConstraints.WEST;
		c.gridwidth = 1;

		gridbag.setConstraints(comboMenu, c);
		panel.add(comboMenu);

		count = 0;
	}

	public void addComponent(JComponent component) {
		c.gridx = ++count;
		gridbag.setConstraints(component, c);
		panel.add(component);
	}

	/**
	 * Returns the filterAction.
	 * 
	 * @return FilterAction
	 */
	public IFilterAction getFilterAction() {
		return filterAction;
	}

	/**
	 * Sets the filterAction.
	 * 
	 * @param filterAction
	 *            The filterAction to set
	 */
	public void setFilterAction(IFilterAction filterAction) {
		this.filterAction = filterAction;
	}

	/**
	 * @return Returns the mediator.
	 */
	public IFrameMediator getMediator() {
		return mediator;
	}
}